package com.material.controller.supplier;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.material.common.annotation.LoginUser;
import com.material.common.base.CommonController;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.entity.ResultResponse;
import com.material.common.utils.PinyinUtils;
import com.material.common.utils.SecurityUtils;
import com.material.common.utils.bean.BeanUtil;
import com.material.common.utils.uuid.IdUtils;
import com.material.factory.domain.FactoryInfo;
import com.material.factory.service.IFactoryInfoService;
import com.material.request.AssociationAccountRequest;
import com.material.request.SupplierAddRequest;
import com.material.request.SupplierInfoQueryRequest;
import com.material.request.SupplierUpdateRequest;
import com.material.request.UpdatePwdRequest;
import com.material.response.AccountResponse;
import com.material.supplier.domain.SupplierAccount;
import com.material.supplier.domain.SupplierInfo;
import com.material.supplier.service.ISupplierAccountService;
import com.material.supplier.service.ISupplierInfoService;
import com.material.supplier.vo.SupplierAccountVo;
import com.material.supplier.vo.SupplierInfoVo;
import com.material.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ganxinge
 */
@Api(tags = "2.供应商账号信息")
@RestController
@RequestMapping("/account")
public class SupplierAccountController extends CommonController {

    @Autowired
    private ISupplierInfoService supplierInfoService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISupplierAccountService supplierAccountService;
    @Autowired
    private IFactoryInfoService factoryInfoService;

    @ApiOperation("获取当前登录供应商信息")
    @GetMapping("/info")
    public ResultResponse<AccountResponse> accountInfo(@ApiIgnore @LoginUser SysUser sysUser){
        checkSupplierUser(sysUser);
        SupplierInfo supplierInfo = supplierInfoService.selectSupplierInfoById(sysUser.getBizId());
        AccountResponse response = BeanUtil.copyProperties(supplierInfo, AccountResponse.class);
        return ResultResponse.success(response);
    }

    @ApiOperation("修改密码")
    @PostMapping("/updatePwd")
    public ResultResponse updatePwd(@ApiIgnore @LoginUser SysUser sysUser,@RequestBody UpdatePwdRequest updatePwdRequest){
        String pwd = updatePwdRequest.getPwd();
        String password = sysUser.getPassword();
        String s = SecurityUtils.encryptPassword(pwd);
        if(!s.equals(password)){
            return ResultResponse.error("原密码错误");
        }
        SysUser  update = new SysUser();
        update.setUserId(sysUser.getUserId());
        update.setPassword(SecurityUtils.encryptPassword(updatePwdRequest.getNewPwd()));
        sysUserService.updatePassword(update);
        return ResultResponse.success();
    }

    @ApiOperation("修改供应商信息")
    @PostMapping("/updateSupplierInfo")
    public ResultResponse updateSupplierInfo(@ApiIgnore @LoginUser SysUser sysUser,@RequestBody SupplierUpdateRequest supplierUpdateRequest){
        checkSupplierUser(sysUser);
        SupplierInfo supplierInfo = BeanUtil.copyProperties(supplierUpdateRequest, SupplierInfo.class);
        supplierInfo.setAuthStatus(1);
        supplierInfo.setId(sysUser.getBizId());
        supplierInfoService.updateSupplierInfo(supplierInfo);
        return ResultResponse.success();
    }

    @ApiOperation("我的供应商列表")
    @GetMapping("/mySupplierList")
    public ResultResponse mySupplierList(@ApiIgnore @LoginUser SysUser sysUser,SupplierInfoQueryRequest supplierInfoQueryRequest){
        //checkSupplierUser(sysUser);
        SupplierInfo query = new SupplierInfo();
        String userType = sysUser.getUserType();
        if(SysUser.USER_TYPE_1.equals(userType) || SysUser.USER_TYPE_2.equals(userType)){
            query.setFactoryId(sysUser.getBizId());
        }
        if(SysUser.USER_TYPE_3.equals(userType)){
            query.setParentId(sysUser.getBizId());

        }
        PageHelper.startPage(supplierInfoQueryRequest.getPageNum(),supplierInfoQueryRequest.getPageSize());
        List<SupplierInfoVo> supplierInfos = supplierInfoService.selectSupplierList(query);
        PageInfo<SupplierInfoVo> pageInfo = new PageInfo<>(supplierInfos);
        return ResultResponse.success(pageInfo);
    }

    @ApiOperation("添加二级供应商账号")
    @PostMapping("/addSupplier")
    public ResultResponse addSupplier(@ApiIgnore @LoginUser SysUser sysUser,@RequestBody SupplierAddRequest supplierAddRequest){
        SupplierInfo mineSupplier = supplierInfoService.selectSupplierInfoById(sysUser.getBizId());
        String firstLetters = PinyinUtils.getFirstLetters(supplierAddRequest.getSupplierName());
        String loginName = firstLetters + supplierAddRequest.getMobile();
        SupplierInfo supplierInfo = new SupplierInfo();
        supplierInfo.setParentId(sysUser.getBizId());
        supplierInfo.setFactoryId(mineSupplier.getFactoryId());
        supplierInfo.setLoginName(loginName);
        supplierInfo.setName(supplierAddRequest.getSupplierName());
        supplierInfo.setRealName(supplierAddRequest.getRealName());
        supplierInfo.setMobile(supplierAddRequest.getMobile());
        supplierInfo.setCreateTime(new Date());
        supplierInfoService.insertSupplierInfo(supplierInfo);
        return ResultResponse.success();
    }

    @ApiOperation("我的账号列表")
    @GetMapping("/myAccountList")
    public ResultResponse myAccountList(@ApiIgnore @LoginUser SysUser sysUser){
        checkSupplierUser(sysUser);
        SupplierAccount supplierAccount = supplierAccountService.selectSupplierAccountBySupplierId(sysUser.getBizId());
        if(supplierAccount == null){
            return ResultResponse.success();
        }
        String supplierGroup = supplierAccount.getSupplierGroup();
        SupplierAccount query = new SupplierAccount();
        query.setSupplierGroup(supplierGroup);
        List<SupplierAccountVo> supplierAccounts = supplierAccountService.selectSupplierAccountVoList(query);
        return ResultResponse.success(supplierAccounts);
    }

    @ApiOperation("关联账号")
    @PostMapping("/associationAccount")
    public ResultResponse associationAccount(@ApiIgnore @LoginUser SysUser sysUser,@RequestBody AssociationAccountRequest associationAccountRequest){
        String loginName = associationAccountRequest.getLoginName();
        SupplierAccount exist = supplierAccountService.selectSupplierAccountByLoginName(loginName);
        if(exist != null){
            return ResultResponse.error("该账户已经关联了！");
        }
        SysUser dbUser = sysUserService.selectUserByUserName(loginName);
        if(dbUser == null){
            return ResultResponse.error("要关联的账号不存在");
        }
        String password = dbUser.getPassword();
        String s = SecurityUtils.encryptPassword(associationAccountRequest.getPwd());
        if(!s.equals(password)){
            return ResultResponse.error("输入密码错误");
        }
        Long supplierId = sysUser.getBizId();
        SupplierAccount supplierAccount = supplierAccountService.selectSupplierAccountBySupplierId(supplierId);
        List<SupplierAccount> addAccount = new ArrayList<>();

        SupplierInfo addSupplier = supplierInfoService.selectSupplierInfoById(dbUser.getBizId());
        if(addSupplier == null){
            return ResultResponse.error("数据异常，请联系管理员");
        }
        SupplierAccount a = new SupplierAccount();
        a.setSupplierId(addSupplier.getId());
        a.setLoginName(addSupplier.getLoginName());
        a.setSupplierName(addSupplier.getName());
        a.setCreateTime(new Date());

        if(supplierAccount != null){
            FactoryInfo factoryInfo = factoryInfoService.selectFactoryInfoById(addSupplier.getFactoryId());
            //当前登录的账号已经关联了，拿到关联的分组编号
            String supplierGroup = supplierAccount.getSupplierGroup();
            a.setSupplierGroup(supplierGroup);
            a.setFactoryName(factoryInfo.getName());
            a.setFactoryId(addSupplier.getFactoryId());
            addAccount.add(a);
        }else{

            //当前登录的号没有关联
            String supplierGroup = IdUtils.fastSimpleUUID();
            //当前登录的账号的供应商
            SupplierInfo supplierInfo = supplierInfoService.selectSupplierInfoById(supplierId);
            FactoryInfo factoryInfo = factoryInfoService.selectFactoryInfoById(supplierInfo.getFactoryId());
            String loginName1 = supplierInfo.getLoginName();
            String name = supplierInfo.getName();
            SupplierAccount account = new SupplierAccount();
            account.setSupplierGroup(supplierGroup);
            account.setSupplierId(supplierId);
            account.setLoginName(loginName1);
            account.setSupplierName(name);
            account.setCreateTime(new Date());
            account.setFactoryName(factoryInfo.getName());
            account.setFactoryId(supplierInfo.getFactoryId());

            addAccount.add(account);
            a.setSupplierGroup(supplierGroup);
            if(!supplierId.equals(addSupplier.getId())){
                FactoryInfo addFactory = factoryInfoService.selectFactoryInfoById(addSupplier.getFactoryId());
                a.setFactoryName(addFactory.getName());
                a.setFactoryId(addFactory.getId());
                addAccount.add(a);
            }
        }
        int i = supplierAccountService.batchInsertSupplierAccount(addAccount);
        return ResultResponse.success();
    }


}
